package GUI;

import ModelLayer.*;
import CtrlLayer.*;

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CustomersGUI extends JFrame implements ActionListener{
    private Container container;
    private JPanel panelCustomer;
    private JPanel panelCreateCustomer;
    private JPanel panelFindCustomer;
    private JPanel panelFindCustomer2;
    private JPanel panelUpdateCustomer;
    private JPanel panelUpdateCustomer2;
    private JPanel panelDeleteCustomer;
    private JPanel panelDeleteCustomer2;
    private JButton back;
    private int sessionId;
    private CustomerController customerController;

    public CustomersGUI(int sessionId){
        panelCustomer = new JPanel(new GridBagLayout());
        panelCreateCustomer = new JPanel(new GridBagLayout());
        panelFindCustomer = new JPanel(new GridBagLayout());
        panelFindCustomer2 = new JPanel(new GridBagLayout());
        panelUpdateCustomer = new JPanel(new GridBagLayout());
        panelUpdateCustomer2 = new JPanel(new GridBagLayout());
        panelDeleteCustomer = new JPanel(new GridBagLayout());
        panelDeleteCustomer2 = new JPanel(new GridBagLayout());
        customerController = new CustomerController();
        this.sessionId = sessionId;

        this.setTitle("Vestbjerg company system");
        this.setLocation(300,50);
        this.setVisible(true);
        this.setSize(400, 470);
        this.setResizable(false);

        customersMainMenu();
    }

    public void actionPerformed(ActionEvent e){

    }

    public void makeCustomersMenu(){
        GridBagConstraints gbc = new GridBagConstraints();

        JButton addCustomer = new JButton("Add Customer");
        addCustomer.setPreferredSize(new Dimension(150, 25));
        addCustomer.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    createCustomer();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelCustomer.add(addCustomer, gbc);

        JButton showCustomer = new JButton("Show Customer");
        showCustomer.setPreferredSize(new Dimension(150, 25));
        showCustomer.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findCustomer();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelCustomer.add(showCustomer, gbc);
        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2){
            JButton deleteCustomer = new JButton("Delete Customer");
            deleteCustomer.setPreferredSize(new Dimension(150, 25));
            deleteCustomer.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        container = getContentPane();
                        container.removeAll();
                        deleteCustomerFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            );
            gbc.gridx = 0;
            gbc.gridy = 4;
            gbc.insets = new Insets(5,5,5,5);
            panelCustomer.add(deleteCustomer, gbc);

            JButton updateCustomer = new JButton("Update Customer");
            updateCustomer.setPreferredSize(new Dimension(150, 25));
            updateCustomer.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e){
                        container = getContentPane();
                        container.removeAll();
                        updateCustomerFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                    }
                }
            );
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.insets = new Insets(5,5,5,5);
            panelCustomer.add(updateCustomer, gbc);
        }

        JButton back = new JButton("Close");
        back.setPreferredSize(new Dimension(150, 25));
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    dispose();
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelCustomer.add(back, gbc);
    }

    public void customersMainMenu()
    {
        makeCustomersMenu();
        this.getContentPane().add(panelCustomer, BorderLayout.NORTH);
    }

    private void create()
    {
        JTextField textId = new JTextField();
        JTextField textName = new JTextField();
        JTextField textEmail = new JTextField();
        JTextField textPhoneNumber = new JTextField();
        JTextField textAddress = new JTextField();
        JTextField textDiscount = new JTextField();
        JLabel labelHeader;
        JLabel labelId;
        JLabel labelName;
        JLabel labelEmail;
        JLabel labelPhoneNumber;
        JLabel labelAddress;
        JLabel labelDiscount;
        JLabel errorMessage;
        JButton create;
        JButton clear;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");
        textName.setText("");
        textEmail.setText("");
        textPhoneNumber.setText("");
        textAddress.setText("");
        if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 1){
            textDiscount.setEditable(false);
            textDiscount.setText("0");
        }

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateCustomer.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelCreateCustomer.add(textId,gbc);

        labelName = new JLabel("Name:", JLabel.RIGHT);
        labelName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateCustomer.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textName.setPreferredSize(new Dimension(100,25));
        panelCreateCustomer.add(textName,gbc);

        labelEmail = new JLabel("E-Mail:", JLabel.RIGHT);
        labelEmail.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateCustomer.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textEmail.setPreferredSize(new Dimension(100,25));
        panelCreateCustomer.add(textEmail,gbc);

        labelPhoneNumber = new JLabel("Phone number:", JLabel.RIGHT);
        labelPhoneNumber.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateCustomer.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhoneNumber.setPreferredSize(new Dimension(100,25));
        panelCreateCustomer.add(textPhoneNumber,gbc);

        labelAddress = new JLabel("Address:", JLabel.RIGHT);
        labelAddress.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateCustomer.add(labelAddress, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textAddress.setPreferredSize(new Dimension(100,25));
        panelCreateCustomer.add(textAddress,gbc);

        labelDiscount = new JLabel("Discount:", JLabel.RIGHT);
        labelDiscount.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateCustomer.add(labelDiscount, gbc);

        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        textDiscount.setPreferredSize(new Dimension(100,25));
        panelCreateCustomer.add(textDiscount,gbc);

        errorMessage = new JLabel("", JLabel.RIGHT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        panelCreateCustomer.add(errorMessage, gbc);

        create = new JButton("Create");
        create.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        customerController.createCustomer(Integer.parseInt(textId.getText()),
                            textName.getText(),
                            textEmail.getText(),
                            textPhoneNumber.getText(),
                            textAddress.getText(),
                            Integer.parseInt(textDiscount.getText()));

                        container = getContentPane();
                        panelCreateCustomer.removeAll();
                        container.removeAll();
                        createCustomer();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textId.setText("");
                        textName.setText("");
                        textEmail.setText("");
                        textPhoneNumber.setText("");
                        textAddress.setText("");
                        textDiscount.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        createCustomer();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);

                        textId.setText("");
                        textName.setText("");
                        textEmail.setText("");
                        textPhoneNumber.setText("");
                        textAddress.setText("");
                        textDiscount.setText("");
                        errorMessage.setText("Not possible!");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy =7;
        gbc.insets = new Insets(5,5,5,5);
        create.setPreferredSize(new Dimension(100,25));
        panelCreateCustomer.add(create,gbc);

        clear = new JButton("Clear");
        clear.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    panelCreateCustomer.removeAll();
                    container.removeAll();
                    createCustomer();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    textId.setText("");
                    textName.setText("");
                    textEmail.setText("");
                    textPhoneNumber.setText("");
                    textAddress.setText("");
                    textDiscount.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 7;
        gbc.insets = new Insets(5,5,5,5);
        clear.setPreferredSize(new Dimension(100,25));
        panelCreateCustomer.add(clear,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    textId.setText("");
                    textName.setText("");
                    textEmail.setText("");
                    textPhoneNumber.setText("");
                    textAddress.setText("");
                    textDiscount.setText("");
                    errorMessage.setText("");

                    container = getContentPane();
                    panelCreateCustomer.removeAll();
                    container.removeAll();
                    customersMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                }
            }
        );
        gbc.gridx = 2;
        gbc.gridy = 7;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelCreateCustomer.add(back,gbc);
    }

    public void createCustomer()
    {
        create();
        this.getContentPane().add(panelCreateCustomer, BorderLayout.NORTH);
    }

    private void find()
    {
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelFindCustomer.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelFindCustomer.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.RIGHT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelFindCustomer.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        findCustomerSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        findCustomer();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        errorMessage.setText("No customer!");
                        textId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelFindCustomer.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    customersMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);

                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelFindCustomer.add(back,gbc);
    }

    public void findCustomer()
    {
        find();
        this.getContentPane().add(panelFindCustomer, BorderLayout.NORTH);
    }

    public void findSecondWindow(int id)
    {
        JTextField textId = new JTextField();
        JTextField textName = new JTextField();
        JTextField textEmail = new JTextField();
        JTextField textPhoneNumber = new JTextField();
        JTextField textAddress = new JTextField();
        JTextField textDiscount = new JTextField();
        JLabel labelId;
        JLabel labelName;
        JLabel labelEmail;
        JLabel labelPhoneNumber;
        JLabel labelAddress;
        JLabel labelDiscount;
        JButton ok;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textName.setEditable(false);
        textEmail.setEditable(false);
        textPhoneNumber.setEditable(false);
        textAddress.setEditable(false);
        textDiscount.setEditable(false);

        textId.setText(Integer.toString(id));
        textName.setText(customerController.findCustomer(id).getName());
        textEmail.setText(customerController.findCustomer(id).getEmail());
        textPhoneNumber.setText(customerController.findCustomer(id).getPhoneNumber());
        textAddress.setText(customerController.findCustomer(id).getAddress());
        textDiscount.setText(Integer.toString(customerController.findCustomer(id).getDiscount()));

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelFindCustomer2.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelFindCustomer2.add(textId,gbc);

        labelName = new JLabel("Name:", JLabel.RIGHT);
        labelName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelFindCustomer2.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textName.setPreferredSize(new Dimension(100,25));
        panelFindCustomer2.add(textName,gbc);

        labelEmail = new JLabel("E-Mail:", JLabel.RIGHT);
        labelEmail.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelFindCustomer2.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textEmail.setPreferredSize(new Dimension(100,25));
        panelFindCustomer2.add(textEmail,gbc);

        labelPhoneNumber = new JLabel("Phone number:", JLabel.RIGHT);
        labelPhoneNumber.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelFindCustomer2.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhoneNumber.setPreferredSize(new Dimension(100,25));
        panelFindCustomer2.add(textPhoneNumber,gbc);

        labelAddress = new JLabel("Address:", JLabel.RIGHT);
        labelAddress.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelFindCustomer2.add(labelAddress, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textAddress.setPreferredSize(new Dimension(100,25));
        panelFindCustomer2.add(textAddress,gbc);

        labelDiscount = new JLabel("Discount:", JLabel.RIGHT);
        labelDiscount.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelFindCustomer2.add(labelDiscount, gbc);

        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        textDiscount.setPreferredSize(new Dimension(100,25));
        panelFindCustomer2.add(textDiscount,gbc);

        ok = new JButton("OK");
        ok.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    findCustomer();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        ok.setPreferredSize(new Dimension(100,25));
        panelFindCustomer2.add(ok,gbc);
    }

    public void findCustomerSecondWindow(int id)
    {
        findSecondWindow(id);
        this.getContentPane().add(panelFindCustomer2, BorderLayout.NORTH);
    }

    private void updateFirstWindow()
    {
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateCustomer.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelUpdateCustomer.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.RIGHT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateCustomer.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        updateCustomerSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        updateCustomerFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        errorMessage.setText("No customer!");
                        textId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelUpdateCustomer.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    customersMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelUpdateCustomer.add(back,gbc);
    }

    private void updateSecondWindow(int id)
    {
        JTextField textId = new JTextField();
        JTextField textName = new JTextField();
        JTextField textEmail = new JTextField();
        JTextField textPhoneNumber = new JTextField();
        JTextField textAddress = new JTextField();
        JTextField textDiscount = new JTextField();
        JLabel labelId;
        JLabel labelName;
        JLabel labelEmail;
        JLabel labelPhoneNumber;
        JLabel labelAddress;
        JLabel labelDiscount;
        JButton update;
        JButton cancel;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textId.setText(Integer.toString(id));
        textName.setText(customerController.findCustomer(id).getName());
        textEmail.setText(customerController.findCustomer(id).getEmail());
        textPhoneNumber.setText(customerController.findCustomer(id).getPhoneNumber());
        textAddress.setText(customerController.findCustomer(id).getAddress());
        textDiscount.setText(Integer.toString(customerController.findCustomer(id).getDiscount()));

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateCustomer2.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelUpdateCustomer2.add(textId,gbc);

        labelName = new JLabel("Name:", JLabel.RIGHT);
        labelName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateCustomer2.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textName.setPreferredSize(new Dimension(100,25));
        panelUpdateCustomer2.add(textName,gbc);

        labelEmail = new JLabel("E-Mail:", JLabel.RIGHT);
        labelEmail.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateCustomer2.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textEmail.setPreferredSize(new Dimension(100,25));
        panelUpdateCustomer2.add(textEmail,gbc);

        labelPhoneNumber = new JLabel("Phone number:", JLabel.RIGHT);
        labelPhoneNumber.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateCustomer2.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhoneNumber.setPreferredSize(new Dimension(100,25));
        panelUpdateCustomer2.add(textPhoneNumber,gbc);

        labelAddress = new JLabel("Address", JLabel.RIGHT);
        labelAddress.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateCustomer2.add(labelAddress, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textAddress.setPreferredSize(new Dimension(100,25));
        panelUpdateCustomer2.add(textAddress,gbc);

        labelDiscount = new JLabel("Discount:", JLabel.RIGHT);
        labelDiscount.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelUpdateCustomer2.add(labelDiscount, gbc);

        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        textDiscount.setPreferredSize(new Dimension(100,25));
        panelUpdateCustomer2.add(textDiscount,gbc);

        update = new JButton("Update");
        update.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    customerController.updateCustomer(id, 
                        textName.getText(),
                        textEmail.getText(),
                        textPhoneNumber.getText(),
                        textAddress.getText(),
                        Integer.parseInt(textDiscount.getText()));

                    container = getContentPane();
                    container.removeAll();
                    updateCustomerFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        update.setPreferredSize(new Dimension(100,25));
        panelUpdateCustomer2.add(update,gbc);

        cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    updateCustomerFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        cancel.setPreferredSize(new Dimension(100,25));
        panelUpdateCustomer2.add(cancel,gbc);

    }

    public void updateCustomerFirstWindow()
    {
        updateFirstWindow();
        this.getContentPane().add(panelUpdateCustomer, BorderLayout.NORTH);
    }

    public void updateCustomerSecondWindow(int id)
    {
        updateSecondWindow(id);
        this.getContentPane().add(panelUpdateCustomer2, BorderLayout.NORTH);
    }

    private void deleteFirstWindow()
    {
        JTextField textId = new JTextField();
        JLabel labelId;
        JLabel errorMessage;
        JButton find;
        JButton back;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setText("");

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteCustomer.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelDeleteCustomer.add(textId,gbc);

        errorMessage = new JLabel("", JLabel.RIGHT);
        errorMessage.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteCustomer.add(errorMessage, gbc);

        find = new JButton("Search");
        find.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    try{
                        container = getContentPane();
                        container.removeAll();
                        deleteCustomerSecondWindow(Integer.parseInt(textId.getText()));
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        textId.setText("");
                        errorMessage.setText("");
                    }catch(Exception ex){
                        container = getContentPane();
                        container.removeAll();
                        deleteCustomerFirstWindow();
                        container.invalidate();
                        container.revalidate();
                        container.repaint();
                        container.setVisible(true);
                        errorMessage.setText("No customer!");
                        textId.setText("");
                    }
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        find.setPreferredSize(new Dimension(100,25));
        panelDeleteCustomer.add(find,gbc);

        back = new JButton("Back");
        back.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    customersMainMenu();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                    textId.setText("");
                    errorMessage.setText("");
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        back.setPreferredSize(new Dimension(100,25));
        panelDeleteCustomer.add(back,gbc);
    }

    private void deleteSecondWindow(int id)
    {
        JTextField textId = new JTextField();
        JTextField textName = new JTextField();
        JTextField textEmail = new JTextField();
        JTextField textPhoneNumber = new JTextField();
        JTextField textAddress = new JTextField();
        JTextField textDiscount = new JTextField();
        JLabel labelId;
        JLabel labelName;
        JLabel labelEmail;
        JLabel labelPhoneNumber;
        JLabel labelAddress;
        JLabel labelDiscount;
        JButton delete;
        JButton cancel;
        GridBagConstraints gbc = new GridBagConstraints();

        textId.setEditable(false);
        textName.setEditable(false);
        textEmail.setEditable(false);
        textPhoneNumber.setEditable(false);
        textAddress.setEditable(false);
        textDiscount.setEditable(false);

        textId.setText(Integer.toString(id));
        textName.setText(customerController.findCustomer(id).getName());
        textEmail.setText(customerController.findCustomer(id).getEmail());
        textPhoneNumber.setText(customerController.findCustomer(id).getPhoneNumber());
        textAddress.setText(customerController.findCustomer(id).getAddress());
        textDiscount.setText(Integer.toString(customerController.findCustomer(id).getDiscount()));

        labelId = new JLabel("ID:", JLabel.RIGHT);
        labelId.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteCustomer2.add(labelId, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.insets = new Insets(5,5,5,5);
        textId.setPreferredSize(new Dimension(100,25));
        panelDeleteCustomer2.add(textId,gbc);

        labelName = new JLabel("Name:", JLabel.RIGHT);
        labelName.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteCustomer2.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.insets = new Insets(5,5,5,5);
        textName.setPreferredSize(new Dimension(100,25));
        panelDeleteCustomer2.add(textName,gbc);

        labelEmail = new JLabel("E-Mail:", JLabel.RIGHT);
        labelEmail.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteCustomer2.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.insets = new Insets(5,5,5,5);
        textEmail.setPreferredSize(new Dimension(100,25));
        panelDeleteCustomer2.add(textEmail,gbc);

        labelPhoneNumber = new JLabel("Phone number:", JLabel.RIGHT);
        labelPhoneNumber.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteCustomer2.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.insets = new Insets(5,5,5,5);
        textPhoneNumber.setPreferredSize(new Dimension(100,25));
        panelDeleteCustomer2.add(textPhoneNumber,gbc);

        labelAddress = new JLabel("Address:", JLabel.RIGHT);
        labelAddress.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteCustomer2.add(labelAddress, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.insets = new Insets(5,5,5,5);
        textAddress.setPreferredSize(new Dimension(100,25));
        panelDeleteCustomer2.add(textAddress,gbc);

        labelDiscount = new JLabel("Discount:", JLabel.RIGHT);
        labelDiscount.setPreferredSize(new Dimension(100, 25));
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        panelDeleteCustomer2.add(labelDiscount, gbc);

        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.insets = new Insets(5,5,5,5);
        textDiscount.setPreferredSize(new Dimension(100,25));
        panelDeleteCustomer2.add(textDiscount,gbc);

        delete = new JButton("Delete");
        delete.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    customerController.deleteCustomer(id);
                    container = getContentPane();
                    container.removeAll();
                    deleteCustomerFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        delete.setPreferredSize(new Dimension(100,25));
        panelDeleteCustomer2.add(delete,gbc);

        cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e){
                    container = getContentPane();
                    container.removeAll();
                    deleteCustomerFirstWindow();
                    container.invalidate();
                    container.revalidate();
                    container.repaint();
                    container.setVisible(true);
                }
            }
        );
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.insets = new Insets(5,5,5,5);
        cancel.setPreferredSize(new Dimension(100,25));
        panelDeleteCustomer2.add(cancel, gbc);

    }

    private void deleteCustomerFirstWindow()
    {
        deleteFirstWindow();
        this.getContentPane().add(panelDeleteCustomer, BorderLayout.NORTH);
    }

    private void deleteCustomerSecondWindow(int id)
    {
        deleteSecondWindow(id);
        this.getContentPane().add(panelDeleteCustomer2, BorderLayout.NORTH);
    }
}